package com.googlecode.jmicroerp.wicket.vanzare;

import com.googlecode.jmicroerp.model.Bon;
import com.googlecode.jmicroerp.model.NCategorie;
import com.googlecode.jmicroerp.model.Produs;
import com.googlecode.jmicroerp.model.ProdusVandut;
import com.googlecode.jmicroerp.model.Rol;
import com.googlecode.jmicroerp.model.Stoc;
import com.googlecode.jmicroerp.model.Utilizator;
import com.googlecode.jmicroerp.repository.GenericOrder;
import com.googlecode.jmicroerp.repository.GenericProperty;
import com.googlecode.jmicroerp.repository.GenericRestriction;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.service.BonService;
import com.googlecode.jmicroerp.wicket.SecuredWebSession;
import com.googlecode.jmicroerp.wicket.components.FileResource;
import com.googlecode.jmicroerp.wicket.index.IndexPage;
import com.googlecode.jmicroerp.wicket.login.LoginPage;
import com.googlecode.jmicroerp.wicket.reports.ReportsPage;
import com.googlecode.jmicroerp.wicket.butoaie.ButoaiePage;
import com.googlecode.jmicroerp.wicket.zzz.ZZZListPage;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.security.components.SecureWebPage;

/**
 *
 * @author alex
 */
public class PosPage extends SecureWebPage {

    @SpringBean(name = "genericService")
    private GenericService<NCategorie> categorieService;
    
    @SpringBean(name = "bonService")
    private BonService bonServicea;
    
    private Link createNavigationLink(String id, final Class<? extends SecureWebPage> pageClass) {
        Link link = new Link(id) {
            @Override
            public void onClick() {
                setResponsePage(pageClass);
            }
        };
        return link;
    }
    
    @SpringBean(name = "genericService")
    private GenericService<Stoc> produsService;
    @SpringBean(name = "genericService")
    private GenericService<Bon> bonService;
    @SpringBean(name = "genericService")
    private GenericService<ProdusVandut> produsVandutService;
    @SpringBean(name = "genericService")
    private GenericService<Utilizator> genericService;
    @SpringBean(name = "uploadPath")
    private String uploadPath;
    private List<Stoc> produseVizibile;
    private Map<Long, ProdusVandut> produsePeBon;
    private Bon bon;
    private ProdusVandut produsVandut;
    private Integer indexVandut;
    private NCategorie categorieSelectionata;
    private Produs produsSelectionat;
    private BigDecimal cantitate;
    private BigDecimal multiplier = BigDecimal.ONE;
    private BigDecimal incasat;
    private BigDecimal rest;
    private static final int COLS_PER_ROW = 3;
    private Component panelTop;
    private Component panelBon;
    private Component panelCategorii;
    private Component panelProduse;
    private ProduseView produseView;
    private Label lblTotal;
    private Label lblIncasat;
    private Label lblRest;
    private ProduseVanduteView produseVanduteView;

    class CategorieListView extends ListView<NCategorie> {

        public CategorieListView(String id, List<NCategorie> categorii) {
            super(id, categorii);
        }

        @Override
        public void populateItem(final ListItem<NCategorie> li) {
            final NCategorie categorie = li.getModelObject();
            AjaxLink selCategorie = new AjaxLink("selCategorie") {
                @Override
                public void onClick(AjaxRequestTarget art) {
                    categorieSelectionata = categorie;
                    GenericRestriction resCat = new GenericRestriction(
                            new GenericProperty("recipient")
                            .setReference(new GenericProperty("tipRecipient")
                            .setReference(new GenericProperty("categorie")
                            .setReference(new GenericProperty("id")))), categorieSelectionata.getId());
                    GenericRestriction resMag = new GenericRestriction(
                            new GenericProperty("recipient")
                            .setReference(new GenericProperty("magazin")
                            .setReference(new GenericProperty("id"))), ((SecuredWebSession) getSession()).getMagazin().getId());
                    GenericRestriction resAct = new GenericRestriction(new GenericProperty("produs").setReference(new GenericProperty("activ")), Boolean.TRUE);
                    List<GenericRestriction> res = new ArrayList<GenericRestriction>();
                    res.add(resCat);
                    res.add(resMag);
                    res.add(resAct);                    
                    GenericOrder ordMag = new GenericOrder(new GenericProperty("produs").setReference(new GenericProperty("ordine")), true);
                    produseVizibile = produsService.getList(Stoc.class, res, Arrays.asList(ordMag));
                    produseView.setList(range(getRows()));
                    li.add(new AttributeAppender("class", "active"));
                    art.add(panelProduse, panelCategorii);
                }
            };
            Label lbl = new Label("denumire", categorie.getDenumire());
            selCategorie.add(lbl);
            li.add(selCategorie);
        }
    }

    class ProduseView extends ListView<Integer> {

        private List<ProdusListView> produsListViews;

        public ProduseView(String id, List<Integer> idx) {
            super(id, idx);
            produsListViews = new ArrayList<ProdusListView>();
        }

        @Override
        public void populateItem(ListItem<Integer> li) {
            final int row = li.getModelObject();
            final List<Integer> cList = range(COLS_PER_ROW);
            ProdusListView produsListView = new ProdusListView("col", cList, row);
            produsListViews.add(produsListView);
            li.add(produsListView);
        }

        @Override
        public ListView<Integer> setList(List<? extends Integer> list) {
            ListView<Integer> listView = super.setList(list);
            for (ProdusListView produsListView : produsListViews) {
                produsListView.setList(range(COLS_PER_ROW));
            }

            return listView;
        }
    }

    class ProdusListView extends ListView<Integer> {

        private int row;

        public ProdusListView(String id, List<Integer> idx, int row) {
            super(id, idx);
            this.row = row;
        }

        public int getRow() {
            return row;
        }

        public void setRow(int row) {
            this.row = row;
        }

        @Override
        public void populateItem(ListItem<Integer> li) {
            final Integer col = li.getModelObject();
            final int index = (row * COLS_PER_ROW) + col;
            boolean visible = true;
            if (index < produseVizibile.size()) {
                produseVizibile.get(index).getRecipient().getTipRecipient().getDenumire();
            }
            AjaxLink selProdus = new AjaxLink("selProdus") {
                @Override
                public void onClick(AjaxRequestTarget art) {
                    multiplier = BigDecimal.ONE;
                    indexVandut = index;
                    produsSelectionat = produseVizibile.get(index).getProdus();
                    produsVandut = produsePeBon.get(produsSelectionat.getId());
                    // cantitate = new BigDecimal("1");
                    if (produseVizibile.get(index).getRecipient().getTipRecipient().getVanzareFranctionata()) {
                        cantitate = new BigDecimal("0.5");
                    }
                    else {
                        cantitate = new BigDecimal("1");
                    }
                    if (produsVandut == null) {
                        produsVandut = new ProdusVandut();
                        produsVandut.setCantitate(cantitate);
                        produsVandut.setPret(produsSelectionat.getPret());
                        produsVandut.setProdus(produsSelectionat);
                        bon.getProduse().add(produsVandut);
                        produsePeBon.put(produsSelectionat.getId(), produsVandut);
                    } else {
                        produsVandut.setCantitate(produsVandut.getCantitate().add(cantitate));
                    }
                    // bon.setValoareTotala(bon.getValoareTotala().add(produsSelectionat.getPret().multiply(new BigDecimal("0.5"))));
                    bon.setValoareTotala(bon.getValoareTotala().add(produsSelectionat.getPret().multiply(cantitate)));
                    lblTotal.setDefaultModelObject(new DecimalFormat("0.00").format(bon.getValoareTotala().doubleValue()));
                    lblTotal.modelChanged();
                    produseVanduteView.modelChanged();
                    art.add(panelProduse);
                    art.add(panelBon);
                }
            };
            selProdus.add(new Label("denumire", (produseVizibile.size() > index) ? produseVizibile.get(index).getProdus().getDenumire() : ""));
            selProdus.add(new Label("cantitate", (produseVizibile.size() > index) ? produseVizibile.get(index).getCantitate() : ""));
            String caleImagine = "";
            if (index >= produseVizibile.size()) {
                caleImagine += "noproduct.png";
                visible = false;
            } else if (produseVizibile.get(index).getProdus().getCaleImagine() != null) {
                caleImagine += produseVizibile.get(index).getProdus().getCaleImagine();
            } else {
                caleImagine += "noimage.jpg";
            }
            if ((index < produseVizibile.size())) {
                BigDecimal cantitateRamasa = produseVizibile.get(index).getCantitate();
                ProdusVandut produsVandut = produsePeBon.get(produseVizibile.get(index).getProdus().getId());
                if (produsVandut != null) {
                    cantitateRamasa = cantitateRamasa.subtract(produsVandut.getCantitate());
                }

                if (cantitateRamasa.doubleValue() <= 0) {
                    selProdus.setEnabled(false);
                }
            }
            Image img = new Image("caleImagine", new FileResource(uploadPath, caleImagine)) {

                @Override
                protected boolean shouldAddAntiCacheParameter() {
                    return true;
                }
                
            };
            selProdus.add(img);
            selProdus.setVisible(visible);
            li.add(selProdus);
        }
    }

    class ProduseVanduteView extends ListView<ProdusVandut> {

        public ProduseVanduteView(String id, List<ProdusVandut> produse) {
            super(id, produse);
        }

        @Override
        public void populateItem(ListItem<ProdusVandut> li) {
            ProdusVandut produsVandut = li.getModelObject();
            Label lblIndex = new Label("idx", (li.getIndex() + 1));
            Label lblDenumire = new Label("denumire", produsVandut.getProdus().getDenumire());
            Label lblPret = new Label("pret", produsVandut.getPret());
            Label lblCantitate = new Label("cantitate", produsVandut.getCantitate());

            li.add(lblIndex, lblDenumire, lblCantitate, lblPret);
        }
    }

    private Component getPanelTop() {
        WebMarkupContainer container = new WebMarkupContainer("topInfo");
        container.setOutputMarkupId(true);
        return container;
    }
    
    private void digitPressed(Integer value, AjaxRequestTarget target) {
        if (produsVandut != null) {
            if (cantitate.doubleValue() < 1) {
                cantitate = BigDecimal.ZERO;
            }
            cantitate = getCantitate(cantitate, BigDecimal.TEN, BigDecimal.valueOf(value));
            produsVandut.setCantitate(cantitate);
            recomputeBon(bon);
            target.add(panelBon);
        }
    }

    private Component getPanelBon() {
        WebMarkupContainer container = new WebMarkupContainer("bon");
        container.setOutputMarkupId(true);
        
        final Label lblVandAzi = new Label("vanzariAzi", bonServicea.queryVanzariAziByMagazin(((SecuredWebSession) getSession()).getMagazin().getId()).toString());
        container.add(lblVandAzi);
        
        lblTotal = new Label("total", bon.getValoareTotala());
        lblIncasat = new Label("incasat", incasat);
        lblRest = new Label("rest", getRest(incasat, bon.getValoareTotala()));
        container.add(lblTotal);
        container.add(lblIncasat);
        container.add(lblRest);
        produseVanduteView = new ProduseVanduteView("produseBon", bon.getProduse());
        container.add(produseVanduteView);

        container.add(new AjaxLink("btn7") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(7, target);
            }
        });
        container.add(new AjaxLink("btn8") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(8, target);
            }
        });
        container.add(new AjaxLink("btn9") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(9, target);
            }
        });
        container.add(new AjaxLink("btn4") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(4, target);
            }
        });
        container.add(new AjaxLink("btn5") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(5, target);
            }
        });
        container.add(new AjaxLink("btn6") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(6, target);
            }
        });
        container.add(new AjaxLink("btn1") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(1, target);
            }
        });
        container.add(new AjaxLink("btn2") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(2, target);
            }
        });
        container.add(new AjaxLink("btn3") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(3, target);
            }
        });
        container.add(new AjaxLink("btn0") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                digitPressed(0, target);
            }
        });
        container.add(new AjaxLink("btn00") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                if (produsVandut != null) {
                    if (cantitate.doubleValue() < 1) {
                        cantitate = BigDecimal.ZERO;
                    }
                    cantitate = getCantitate(cantitate, BigDecimal.valueOf(100), BigDecimal.valueOf(0));
                    produsVandut.setCantitate(cantitate);
                    recomputeBon(bon);
                    target.add(panelBon);
                }
            }
        });
        container.add(new AjaxLink("btnV") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                multiplier = new BigDecimal("0.1");
            }
        });

        container.add(new AjaxLink("pay") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                if (!bon.getProduse().isEmpty()) {
                    for (ProdusVandut produsVandut : bon.getProduse()) {
                        produsVandutService.insert(produsVandut);
                        GenericRestriction restriction = new GenericRestriction(new GenericProperty("produs").setReference(new GenericProperty("id")), produsVandut.getProdus().getId());
                        GenericRestriction resMag = new GenericRestriction(
                                new GenericProperty("recipient")
                                .setReference(new GenericProperty("magazin")
                                .setReference(new GenericProperty("id"))), ((SecuredWebSession) getSession()).getMagazin().getId());
                        Stoc stoc = produsService.getSingleResult(Stoc.class, restriction, resMag);
                        stoc.setCantitate(stoc.getCantitate().subtract(produsVandut.getCantitate()));
                        produsService.update(stoc);
                    }
                    bon.setDataVanzarii(new Date());
                    bonService.insert(bon);
                    produsePeBon.clear();
                    bon = new Bon();
                    bon.setMagazin(((SecuredWebSession) getSession()).getMagazin());
                    bon.setUtilizator(((SecuredWebSession) getSession()).getUserDetail().getUtilizator());
                    bon.setValoareTotala(BigDecimal.ZERO);
                    bon.setProduse(new ArrayList<ProdusVandut>());
                    produsVandut = null;
                    cantitate = BigDecimal.ZERO;
                    produsSelectionat = null;
                    multiplier = BigDecimal.ONE;
                    
                    lblVandAzi.setDefaultModelObject(bonServicea.queryVanzariAziByMagazin(((SecuredWebSession) getSession()).getMagazin().getId()).toString());
                    lblVandAzi.modelChanged();
                    
                    lblTotal.setDefaultModelObject(bon.getValoareTotala());
                    lblTotal.modelChanged();
                    lblRest.setDefaultModelObject(getRest(incasat, bon.getValoareTotala()));
                    lblRest.modelChanged();
                    produseVanduteView.setList(bon.getProduse());
                    produseVanduteView.modelChanged();
                    

                    List<GenericRestriction> restrictions = new ArrayList<GenericRestriction>();

                    if (categorieSelectionata != null) {
                        GenericRestriction res = new GenericRestriction(
                                new GenericProperty("recipient")
                                .setReference(new GenericProperty("tipRecipient")
                                .setReference(new GenericProperty("categorie")
                                .setReference(new GenericProperty("id")))), categorieSelectionata.getId());
                        restrictions.add(res);
                    }
                
                    GenericRestriction resMag = new GenericRestriction(
                                new GenericProperty("recipient")
                                .setReference(new GenericProperty("magazin")
                                .setReference(new GenericProperty("id"))), ((SecuredWebSession) getSession()).getMagazin().getId());
                    restrictions.add(resMag);
                    
                    GenericRestriction resAct = new GenericRestriction(new GenericProperty("produs").setReference(new GenericProperty("activ")), Boolean.TRUE);
                    restrictions.add(resAct);
                    GenericOrder ordMag = new GenericOrder(new GenericProperty("produs").setReference(new GenericProperty("ordine")), true);
                    produseVizibile = produsService.getList(Stoc.class, restrictions, Arrays.asList(ordMag));
                    produseView.setList(range(getRows()));

                    target.add(panelBon, panelProduse);
                }
            }
        });
        container.add(new AjaxLink("bck") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                bon.getProduse().add(produsVandut);
                bon.setValoareTotala(bon.getValoareTotala().add(produsVandut.getCantitate().multiply(produsVandut.getPret())));
                produsVandut = new ProdusVandut();
                cantitate = BigDecimal.ZERO;
                produsSelectionat = null;
                target.add(panelBon);
            }
        }.setEnabled(false));
        container.add(new AjaxLink("add") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                if (indexVandut != null) {
                    BigDecimal addCantitate = (produseVizibile.get(indexVandut).getRecipient().getTipRecipient().getVanzareFranctionata()) ? new BigDecimal("0.5") : BigDecimal.ONE;
                    produsVandut.setCantitate(produsVandut.getCantitate().add(addCantitate));
                    recomputeBon(bon);
                    target.add(panelBon);
                }
            }
        });
        container.add(new AjaxLink("del") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                if (indexVandut != null) {
                    BigDecimal delCantitate = (produseVizibile.get(indexVandut).getRecipient().getTipRecipient().getVanzareFranctionata()) ? new BigDecimal("0.5") : BigDecimal.ONE;
                    produsVandut.setCantitate(produsVandut.getCantitate().subtract(delCantitate));
                    if (produsVandut.getCantitate().compareTo(new BigDecimal("0.5")) < 0) {
                        bon.getProduse().remove(produsVandut);
                        produsePeBon.remove(produsVandut.getProdus().getId());
                        if (bon.getProduse().isEmpty()) {
                            produsVandut = null;
                            produsSelectionat = null;
                        } else {
                            produsVandut = bon.getProduse().get(bon.getProduse().size() - 1);
                            produsSelectionat = produsVandut.getProdus();
                        }
                        cantitate = BigDecimal.ZERO;
                        multiplier = BigDecimal.ONE;
                    }
                    recomputeBon(bon);

                    lblTotal.setDefaultModelObject(bon.getValoareTotala());
                    lblTotal.modelChanged();
                    lblRest.setDefaultModelObject(getRest(incasat, bon.getValoareTotala()));
                    lblRest.modelChanged();
                    produseVanduteView.setList(bon.getProduse());
                    produseVanduteView.modelChanged();
                    target.add(panelBon);
                }
            }
        });
        container.add(new AjaxLink("clear") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                if (produsVandut != null) {
                    bon.getProduse().remove(produsVandut);
                    produsePeBon.remove(produsVandut.getProdus().getId());
                    recomputeBon(bon);
                    if (bon.getProduse().isEmpty()) {
                        produsVandut = null;
                        produsSelectionat = null;
                    } else {
                        produsVandut = bon.getProduse().get(bon.getProduse().size() - 1);
                        produsSelectionat = produsVandut.getProdus();
                    }
                    cantitate = BigDecimal.ZERO;
                    multiplier = BigDecimal.ONE;
                
                    lblTotal.setDefaultModelObject(bon.getValoareTotala());
                    lblTotal.modelChanged();
                    lblRest.setDefaultModelObject(getRest(incasat, bon.getValoareTotala()));
                    lblRest.modelChanged();
                    produseVanduteView.setList(bon.getProduse());
                    produseVanduteView.modelChanged();
                    
                    target.add(panelBon);
                }
            }
        });
        container.add(new AjaxLink("void") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                produsePeBon.clear();
                bon = new Bon();
                bon.setValoareTotala(BigDecimal.ZERO);
                bon.setProduse(new ArrayList<ProdusVandut>());
                bon.setMagazin(((SecuredWebSession) getSession()).getMagazin());
                bon.setUtilizator(((SecuredWebSession) getSession()).getUserDetail().getUtilizator());
                produsVandut = null;
                cantitate = BigDecimal.ZERO;
                produsSelectionat = null;
                multiplier = BigDecimal.ONE;

                lblTotal.setDefaultModelObject(bon.getValoareTotala());
                lblTotal.modelChanged();
                lblRest.setDefaultModelObject(getRest(incasat, bon.getValoareTotala()));
                lblRest.modelChanged();
                produseVanduteView.setList(bon.getProduse());
                produseVanduteView.modelChanged();

                target.add(panelBon);
            }
        });
        return container;
    }

    private Component getPanelCategorii() {
        WebMarkupContainer container = new WebMarkupContainer("categorii");
        container.setOutputMarkupId(true);
        CategorieListView categorieListView = new CategorieListView("categorie", categorieService.getList(NCategorie.class, null, null));
        container.add(categorieListView);
        final WebMarkupContainer catToate = new WebMarkupContainer("catToate");
        AjaxLink selToate = new AjaxLink("selToate") {
            @Override
            public void onClick(AjaxRequestTarget art) {
                categorieSelectionata = null;
                GenericRestriction resMag = new GenericRestriction(
                        new GenericProperty("recipient")
                        .setReference(new GenericProperty("magazin")
                        .setReference(new GenericProperty("id"))), ((SecuredWebSession) getSession()).getMagazin().getId());
                List<GenericRestriction> res = new ArrayList<GenericRestriction>();
                res.add(resMag);
                GenericOrder ordMag = new GenericOrder(new GenericProperty("produs").setReference(new GenericProperty("ordine")), true);
                produseVizibile = produsService.getList(Stoc.class, res, Arrays.asList(ordMag));
                produseView.setList(range(getRows()));
                catToate.add(new AttributeAppender("class", "active"));
                art.add(panelProduse, panelCategorii);
            }
        };
        catToate.add(selToate);
        container.add(catToate);
        return container;
    }

    private int getRows() {
        int size = produseVizibile.size();
        return (size / COLS_PER_ROW) + (((size % COLS_PER_ROW) != 0) ? 1 : 0);
    }

    private Component getPanelProduse() {
        int rows = getRows();

        WebMarkupContainer container = new WebMarkupContainer("produse");
        container.setOutputMarkupId(true);
        produseView = new ProduseView("rows", range(rows));
        container.add(produseView);
        return container;
    }

    public PosPage() {
        Label lblStorename = new Label("storename", "Magazin: " + ((SecuredWebSession) getSession()).getMagazin().getDenumire());
        Label lblNumeUtilizator = new Label("username", ((SecuredWebSession) getSession()).getUserDetail().getUsername());
       // final Label lblVandAzi = new Label("vanzariAzi", bonServicea.queryVanzariAzi());
        Link homeLink = new Link("homeLink") {
            @Override
            public void onClick() {
                String username = ((SecuredWebSession) getSession()).getUserDetail().getUsername();
                GenericRestriction genericRestriction = new GenericRestriction("numeUtilizator", username);
                    Utilizator utilizatorDb = genericService.getSingleResult(Utilizator.class, genericRestriction);
                    for (Rol rol : utilizatorDb.getRoluri()) {
                        if (!"ROLE_USER".equals(rol.getDenumire())) {
                            setResponsePage(IndexPage.class);
                            return;
                        }
                    }
                    setResponsePage(PosPage.class);
            }
        };
        
        
        Link logoutLink = new Link("logoutLink") {
            @Override
            public void onClick() {
                getSession().invalidateNow();
                setResponsePage(LoginPage.class);
            }
        };
        Link linkReports = createNavigationLink("reports", ReportsPage.class); 
        Link linkButoaie = createNavigationLink("butoaie", ButoaiePage.class); 
        Link linkZZZ = createNavigationLink("zzz", ZZZListPage.class); 
        
        add(lblStorename, lblNumeUtilizator, logoutLink, homeLink, linkReports, linkButoaie, linkZZZ);
        /* add(lblStorename, lblNumeUtilizator, logoutLink, homeLink); */

        bon = new Bon();
        bon.setProduse(new ArrayList<ProdusVandut>());
        bon.setValoareTotala(BigDecimal.ZERO);
        bon.setMagazin(((SecuredWebSession) getSession()).getMagazin());
        bon.setUtilizator(((SecuredWebSession) getSession()).getUserDetail().getUtilizator());
        GenericRestriction resMag = new GenericRestriction(
                new GenericProperty("recipient")
                .setReference(new GenericProperty("magazin")
                .setReference(new GenericProperty("id"))), ((SecuredWebSession) getSession()).getMagazin().getId());
        List<GenericRestriction> res = new ArrayList<GenericRestriction>();
        res.add(resMag);
        GenericRestriction resAct = new GenericRestriction(new GenericProperty("produs").setReference(new GenericProperty("activ")), Boolean.TRUE);
        res.add(resAct);
        GenericOrder ordMag = new GenericOrder(new GenericProperty("recipient").setReference(new GenericProperty("ordine")), true);
        produseVizibile = produsService.getList(Stoc.class, res, Arrays.asList(ordMag));
        produsePeBon = new HashMap<Long, ProdusVandut>();

        setOutputMarkupId(true);
        panelTop = getPanelTop();
        panelBon = getPanelBon();
        panelCategorii = getPanelCategorii();
        panelProduse = getPanelProduse();

        add(panelTop);
        add(panelBon);
        add(panelCategorii);
        add(panelProduse);

//        add(new AjaxLink("ron1") {});
//        add(new AjaxLink("ron5") {});
//        add(new AjaxLink("ron10") {});
//        add(new AjaxLink("ron50") {});
//        add(new AjaxLink("ron100") {});
//        add(new AjaxLink("ron200") {});
//        add(new AjaxLink("ron500") {});
//        add(new AjaxLink("bon") {});
//        add(new AjaxLink("card") {});
    }

    private BigDecimal getCantitate(BigDecimal can, BigDecimal multiply, BigDecimal add) {
        if (multiplier.doubleValue() < 1) {
            BigDecimal result = can.add(add.multiply(multiplier));
            multiplier = BigDecimal.ZERO;
            return result;
        } else {
            return can.multiply(multiply).add(add);
        }
    }

    private void recomputeBon(Bon bon) {
        BigDecimal valoareBon = BigDecimal.ZERO;

        for (ProdusVandut produs : bon.getProduse()) {
            valoareBon = valoareBon.add(produs.getPret().multiply(produs.getCantitate()));
        }

        bon.setValoareTotala(valoareBon);
        lblTotal.setDefaultModelObject(new DecimalFormat("0.00").format(bon.getValoareTotala().doubleValue()));
        lblTotal.modelChanged();
        produseVanduteView.modelChanged();

    }

    private List<Integer> range(int value) {
        final List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < value; i++) {
            list.add(i);
        }
        return list;
    }

    private BigDecimal getRest(BigDecimal incasat, BigDecimal pret) {
        if (incasat == null) {
            return pret;
        }
        if (pret == null) {
            return incasat;
        }
        return incasat.subtract(pret);
    }
}
